package com.auction.system.action.auction;

import com.auction.system.base.BaseResponse;
import com.auction.system.constant.SystemConstant;
import com.auction.system.domain.dto.auction.BookCategory;
import com.auction.system.domain.query.auction.BookCategoryQuery;
import com.auction.system.enums.EnumReturnResult;
import com.auction.system.service.auction.IBookCategoryService;
import com.auction.system.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * @ClassName BookCategoryAction
 * @Author 王伟学
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/category")
public class BookCategoryAction {

    @Autowired
    private IBookCategoryService bookCategoryService;

    /**
     * 进入图书类别列表页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return "category/list";
    }

    /**
     * 获取图书类别数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public BookCategoryQuery getList(BookCategoryQuery query) {
        try {
            query = bookCategoryService.query(query);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取图书类别列表数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setCode("获取图书类别数据异常");
        }
        return query;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String preAdd(){
        return "category/add";
    }

    /**
     * 新增图书类别
     * @param bookCategory
     * @param result
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BaseResponse add(@Validated BookCategory bookCategory, BindingResult result){
        BaseResponse response = new BaseResponse();
        try{
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("BookCategory-add参数有误");
                log.info("BookCategory-add参数有误："+result.getAllErrors().toString());
                return response;
            }
            //校验类别编号是否存在
            BookCategoryQuery bookCategoryQuery = new BookCategoryQuery();
            bookCategoryQuery.setCategoryCode(bookCategory.getCategoryCode());
            if(StringUtil.isNotEmpty(bookCategoryService.getList(bookCategoryQuery))){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("该图书类别已存在");
                return response;
            }
            bookCategoryService.add(bookCategory);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("新增图书类别成功");
        }catch (Exception e){
            log.error("新增图书类别异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("新增图书类别失败");
        }
        return response;
    }

    /**
     * 进入修改页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView preEdit(Long id){
        ModelAndView mav = new ModelAndView();
        try{
            BookCategory bookCategory = bookCategoryService.get(id);
            mav.addObject("bookCategory",bookCategory);
            mav.setViewName("category/edit");
        }catch (Exception e){
            log.error("获取图书类别详情失败",e);
            mav.addObject(SystemConstant.ERROR_KEY,"系统异常");
            mav.setViewName("category/list");
        }
        return mav;
    }

    /**
     *  修改图书类别
     * @param bookCategory
     * @param result
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public BaseResponse edit(@Validated BookCategory bookCategory, BindingResult result){
        BaseResponse response = new BaseResponse();
        try{
            if(result.hasErrors()){
                response.setCode(EnumReturnResult.RETURN_404.getCode());
                response.setMsg("BookCategory-edit参数有误");
                log.info("BookCategory-edit参数有误："+result.getAllErrors().toString());
                return response;
            }
            bookCategoryService.edit(bookCategory);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("修改图书类别信息成功");
        }catch (Exception e){
            log.error("修改图书类别信息失败",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("修改图书类别信息失败");
        }
        return response;
    }

    /**
     * 删除图书类别
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public BaseResponse delete(Long id){
        BaseResponse response = new BaseResponse();
        try{
            bookCategoryService.delete(id);
            response.setCode(EnumReturnResult.RETURN_200.getCode());
            response.setMsg("删除图书类别成功");
        }catch (Exception e){
            log.error("删除图书类别异常",e);
            response.setCode(EnumReturnResult.RETURN_404.getCode());
            response.setMsg("删除图书类别失败");
        }
        return response;
    }

    /**
     * 获取图书类别数据
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllList",method = RequestMethod.POST)
    public BookCategoryQuery getAllList(BookCategoryQuery query) {
        try {
            List<BookCategory> bookCategories = bookCategoryService.getList(query);
            query.setData(bookCategories);
            query.setCode(EnumReturnResult.RETURN_200.getCode());
        } catch (Exception e) {
            log.error("获取图书类别数据异常", e);
            query.setCode(EnumReturnResult.RETURN_404.getCode());
            query.setMsg("获取图书类别编号异常");
        }
        return query;
    }

}
